/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.group.service.impl;

import java.sql.Connection;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import net.comm.check.service.impl.JifenServiceImpl;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.common.common;

import net.comm.group.bean.BaseGdianping;
import net.comm.group.bean.BaseGdpGroupby;
import net.comm.group.bean.BaseGdpbyUserId;
import net.comm.group.db.DbGdianpingService;
import net.comm.group.db.DbGdpGbyDsortIDService;
import net.comm.group.db.DbGdpGroupbyService;
import net.comm.group.db.GdianpingServiceSqlMysql;
import net.comm.group.web.form.GForumForm;
import net.comm.group.web.form.GPictureForm;
import net.comm.group.web.form.GdianpingForm;
import net.comm.group.web.form.GdpTotalForm;
import net.comm.group.web.form.GpartyForm;
import net.comm.page.PageCtr;

import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GdianpingServiceImpl {

	static Logger logger = Logger.getLogger(GdianpingServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(GdianpingForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[0] = theForm.getQryDsort();
			fields2[1] = theForm.getQryDsortID();
			
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql3();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[4];
			
			fields[0] = theForm.getQryDsort();
			fields[1] = theForm.getQryDsortID();
			fields[2] = page.getPos()-1;
			fields[3] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();
			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGdianpingService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List listGroupPic(GdianpingForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getQryGroupID();
			fields[1] = theForm.getPage().getPageSize();
			
			String sql = servSql.getGroupPicSql();

			System.out.println("sql="+ sql);
			
			List tmplist = bSqlServ.dbRead(cn, sql, fields, new DbGdpGroupbyService());
			
			if(tmplist.size() == 0){
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return list;				
				
			}
			int len = tmplist.size();
			BaseGdpGroupby groupby = null;
			
			for(int i=0; i< len; i++){
				
				groupby = (BaseGdpGroupby)tmplist.get(i);
				
				BaseGdpbyUserId byuserID = new BaseGdpbyUserId();
				
				byuserID.setDsort(groupby.getDsort());
				byuserID.setDtypeTotalSum(groupby.getDtypeSum());
				if(groupby.getDtype().equals(GlbFinal.DTYPE_UP)){
					byuserID.setDtypeUpSum(groupby.getDtypeSum());
				}else{
					byuserID.setDtypeDownSum(groupby.getDtypeSum());
				}
				byuserID.setUserID(groupby.getUserID());
				byuserID.setUserNick(groupby.getUserNick());
				byuserID.setUserPicID(groupby.getUserPicID());

				if(i+1 < len){
					groupby = (BaseGdpGroupby)tmplist.get(i + 1);
					if(groupby.getUserID() == byuserID.getUserID()){
						
						byuserID.setDtypeTotalSum(byuserID.getDtypeTotalSum() + groupby.getDtypeSum());
						if(groupby.getDtype().equals(GlbFinal.DTYPE_UP)){
							byuserID.setDtypeUpSum(byuserID.getDtypeUpSum() + groupby.getDtypeSum());
						}else{
							byuserID.setDtypeDownSum(byuserID.getDtypeDownSum() + groupby.getDtypeSum());
						}
						i++;
					}
				}
				list.add(byuserID);
			}
			
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List listGroupParty(GdianpingForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getQryGroupID();
			fields[1] = theForm.getPage().getPageSize();
			
			String sql = servSql.getGroupPartySql();
			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGdpGroupbyService());
			
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}	
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List listGbyDsortID(GdianpingForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getQryDsort();
			fields[1] = theForm.getQryDsortID();
			
			String sql = servSql.getRecBbyDsortIDSql();
			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGdpGbyDsortIDService());
						
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}		
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(GdianpingForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			List list = null;

			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getSeqID();			
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGdianpingService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGdianping base = (BaseGdianping)list.get(0);
				
			BeanUtils.copyProperties(theForm, base);
			
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加回复
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(GdianpingForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//更新顶和踩的总数
			//话题点评
			if(theForm.getDsort().equals(GlbFinal.DSORT_FORUM)){
				
				GForumServiceImpl forumservice = new GForumServiceImpl();
				GForumForm forumForm = new GForumForm();
				
				forumForm.setGForumID(theForm.getDsortID());
				if(theForm.getDtype().equals(GlbFinal.DTYPE_UP)){
					forumForm.setUpCnt(1);
				}else{
					forumForm.setDownCnt(1);
				}				
				forumservice.updUpdown(cn, forumForm, msg);
				if(msg.getErrCode() !=0){
					return;
				}
			}else if(theForm.getDsort().equals(GlbFinal.DSORT_PIC)){

				GPictureServiceImpl picservice = new GPictureServiceImpl();
				GPictureForm picForm = new GPictureForm();
				
				picForm.setGpicID(theForm.getDsortID());
				if(theForm.getDtype().equals(GlbFinal.DTYPE_UP)){
					picForm.setUpCnt(1);
				}else{
					picForm.setDownCnt(1);
				}
				picservice.updUpdown(cn, picForm, msg);
				if(msg.getErrCode() !=0){
					return;
				}
			}else if(theForm.getDsort().equals(GlbFinal.DSORT_PARTY)){

				GpartyServiceImpl partyservice = new GpartyServiceImpl();
				GpartyForm partyForm = new GpartyForm();
				partyForm.setPartyID(theForm.getDsortID());
				if(theForm.getDtype().equals(GlbFinal.DTYPE_UP)){
					partyForm.setUpCnt(1);
				}else{
					partyForm.setDownCnt(1);
				}
				partyservice.updsts(cn, partyForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}

				//只允许点评一次
				Object[] fields = new Object[3];
				
				fields[0] = theForm.getDsort();			
				fields[1] = theForm.getDsortID();			
				fields[2] = theForm.getUserID();			
				
				String sql = servSql.getRecbyDsortUserIDSql();			
						
				List list = bSqlServ.dbRead(cn, sql, fields, new DbGdianpingService());
				
				if(list.size() > 0){
					common.setAppFailMsg(msg , "您已经点评了该活动！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;				
				}				
			}
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[8];
			
			theForm.setSeqID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE, GlbFinal.SEQ_DIANPINGID));
			
			fields[0] = theForm.getSeqID();			
			fields[1] = theForm.getGroupID();
			fields[2] = theForm.getDsort();
			fields[3] = theForm.getDsortID();
			fields[4] = theForm.getDtype();
			fields[5] = theForm.getDcont();
			fields[6] = theForm.getUserID();
			fields[7] = DatetimeUtil.getDatetime();
			
			String sql = servSql.getInsertSql();
						
			boolean res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 登记失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			//更新点评统计表
			GdpTotalServiceImpl dptotalservice = new GdpTotalServiceImpl();
			GdpTotalForm dptotalForm = new GdpTotalForm();
			
			dptotalForm.setUserID(theForm.getUserID());
			dptotalForm.setGroupID(theForm.getGroupID());
			dptotalForm.setDsort(theForm.getDsort());
			if(theForm.getDtype().equals(GlbFinal.DTYPE_UP)){
				dptotalForm.setUpNum(1);
			}else{
				dptotalForm.setDownNum(1);
			}
			dptotalForm.setTotal(1);
			
			dptotalservice.update(cn, dptotalForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(GdianpingForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getSeqID();			
			
			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getDeleteSql();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delbydsortID(GdianpingForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			delbydsortID(cn, theForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}	
	/**
	 * 删除指定板块ID的所有点评记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delbydsortID(Connection cn, GdianpingForm theForm, ReturnMessage msg) 
	{
		
		try
		{

			GdianpingServiceSqlMysql servSql = new GdianpingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getDsort();			
			fields[0] = theForm.getDsortID();			
			
			
			String sql = servSql.getDelbydsortIDSql();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
		}
		
		return ;
	}	
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(GdianpingForm theForm, ReturnMessage msg) 
	{
		
		return ;
	}

}
